import {getToken} from "@/utils/token";

export function dateDefaultFormat(timeStr: any) {
    const time = new Date(timeStr);
    return time.getFullYear() + '-' + (time.getMonth() + 1) + '-' + time.getDate();
}

export function dateFormat(timeStr: any, formatStr: string| undefined) {
    const time = new Date(timeStr);
    if (formatStr == 'yyyy-MM-dd hh:mm:ss') {
        return time.getFullYear() + '-' + (time.getMonth() + 1) + '-' + time.getDate() + ' ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();
    }
    return time.getFullYear() + '年' + (time.getMonth() + 1) + '月' + time.getDate() + '日';
}

export function formatTimeAgo(timeString) {
    // 将传入的时间字符串转换为Date对象
    const targetDate = new Date(timeString);
    const currentDate = new Date();

    // 计算时间差（毫秒）
    const diffMs = currentDate - targetDate;

    // 定义时间常量（毫秒）
    const minuteMs = 60 * 1000;      // 1分钟
    const hourMs = 60 * minuteMs;    // 1小时
    const dayMs = 24 * hourMs;       // 1天
    const threeDaysMs = 3 * dayMs;   // 3天

    // 1小时内：显示分钟
    if (diffMs < hourMs) {
        const minutes = Math.floor(diffMs / minuteMs);
        return `${minutes}分钟前`;
    }
    // 24小时内：显示小时
    else if (diffMs < dayMs) {
        const hours = Math.floor(diffMs / hourMs);
        return `${hours}小时前`;
    }
    // 3天内：显示天数
    else if (diffMs <= threeDaysMs) {
        const days = Math.floor(diffMs / dayMs);
        return `${days}天前`;
    }
    // 超过3天：格式化日期（yyyy年mm月dd日）
    else {
        const year = targetDate.getFullYear();
        const month = String(targetDate.getMonth() + 1).padStart(2, '0');
        const day = String(targetDate.getDate()).padStart(2, '0');
        return `${year}年${month}月${day}日`;
    }
}

export const fileUpload = "/api/file/upload/common"
export const getHeaders = () => {
    return {
        "token": getToken(),
        "user-type": 'user'
    }
}
